﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TransactionsDocument.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the TransactionsDocument type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.Documents
{
    using System;
    using System.Diagnostics.Contracts;
    using Common;
    using View;
    using ViewModel;

    /// <summary>
    /// Defines the <seealso cref="TransactionsDocument"/> type.
    /// </summary>
    public class TransactionsDocument : Document
    {
        /// <summary>
        /// Transactions view model
        /// </summary>
        private readonly TransactionsViewModel _viewModel;

        /// <summary>
        /// Initializes a new instance of the <see cref="TransactionsDocument"/> class.
        /// </summary>
        /// <param name="viewModel">
        /// The view model.
        /// </param>
        public TransactionsDocument(TransactionsViewModel viewModel)
            : base("Transactions")
        {
            Contract.Requires<ArgumentNullException>(viewModel != null);
            _viewModel = viewModel;
            View = new TransactionsView { DataContext = _viewModel };
            ContextMenuGroupName = "cmTransaction";

            TransactionsViewModel.RefreshCommand.RegisterPotentialTarget(View.ActiveAwareness, _viewModel.RefreshList);
            TransactionsViewModel.EditCommand.RegisterPotentialTarget(View.ActiveAwareness, _viewModel.EditTransaction, _viewModel.CanEditTransaction);
            TransactionsViewModel.RelateTransactionCommand.RegisterPotentialTarget(View.ActiveAwareness, _viewModel.RelateTransaction, _viewModel.CanRelateTransaction);
            TransactionsViewModel.DeleteCommand.RegisterPotentialTarget(View.ActiveAwareness, _viewModel.DeleteTransaction, _viewModel.CanDeleteTransaction);
            TransactionsViewModel.FilterAccountCommand.RegisterPotentialTarget(View.ActiveAwareness, _viewModel.FilterByAccount);
            TransactionsViewModel.SortCommand.RegisterPotentialTarget(View.ActiveAwareness, _viewModel.Sort);
            TransactionsViewModel.OrderByCommand.RegisterPotentialTarget(View.ActiveAwareness, _viewModel.OrderBy);
            TransactionsViewModel.ByMonthCommand.RegisterPotentialTarget(View.ActiveAwareness, _viewModel.FilterByMonth);
        }

        /// <summary>
        /// Documents the closed.
        /// </summary>
        public override void DocumentClosed()
        {
            TransactionsViewModel.RefreshCommand.UnregisterPotentialTarget(View.ActiveAwareness);
            TransactionsViewModel.EditCommand.UnregisterPotentialTarget(View.ActiveAwareness);
            TransactionsViewModel.RelateTransactionCommand.UnregisterPotentialTarget(View.ActiveAwareness);
            TransactionsViewModel.DeleteCommand.UnregisterPotentialTarget(View.ActiveAwareness);
            TransactionsViewModel.FilterAccountCommand.UnregisterPotentialTarget(View.ActiveAwareness);
            TransactionsViewModel.SortCommand.UnregisterPotentialTarget(View.ActiveAwareness);
            TransactionsViewModel.OrderByCommand.UnregisterPotentialTarget(View.ActiveAwareness);
            TransactionsViewModel.ByMonthCommand.UnregisterPotentialTarget(View.ActiveAwareness);
        }
    }
}